home *** CD-ROM | disk | FTP | other *** search
- Path: colossus.holonet.net!russell
- From: russell@news.mdli.com (Russell Blackadar)
- Newsgroups: comp.lang.c++
- Subject: Re: Needed: Friend function example.
- Date: 15 Jan 1996 19:26:46 GMT
- Organization: HoloNet National Internet Access System: 510-704-1058/modem
- Message-ID: <4de9pm$frn@colossus.holonet.net>
- References: <4d9pm8$shr@news1.usa.pipeline.com>
- NNTP-Posting-Host: jubal.mdli.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- : On Jan 13, 1996 21:55:56 in article <Needed: Friend function example.>,
- : 'Bob Wade <bwade@nando.net>' wrote:
- :
- : >Could someone please provide an example of a friend function which does
- : >not involve istream or ostream, and does not involve a global function?
- : >I am having trouble declaring a friend function which compiles. Making
- : >the class a friend works fine...
-
- Here's a trivial example that is not global, yet compiles:
-
- class A { void foo() {} };
- class B { friend void A::foo(); };
-
- If I define A after B (even with a forward declaration) it won't
- compile. The reason is that A::foo must be known to exist before
- you can declare it a friend. Judging from your description, I
- think you may be able to get your program to compile if you change
- the order of your class definitions.
-
- On the other hand, sometimes it's just not possible to do this.
- (E.g. if my A also needed a friend in B -- they can't both be
- first.) In such a case you have no choice but to grant friendship
- to the entire class. And in practice, that's really not so bad,
- most of the time. It just means you have to look a little wider
- for possible abuses of the friendship -- but not much wider.
- --
- Russell Blackadar, russell@mdli.com
-